<template>
  <div>
    <el-card>
      <el-form :model="reportTemplate" :inline="true" label-width="120px">
        <!--基础表新增-->
        <el-row>
          <el-col :span="8">
            <el-form-item label="指标类型编号" size="small">
              <el-input v-model="reportTemplate.setCode" style="width:200px" disabled placeholder="请输入报表编号" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="指标类型名称" size="small">
              <el-input v-model="reportTemplate.setName" disabled placeholder="请输入报表名称" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-button type="primary" size="small" icon="el-icon-plus" @click="doAdd">新 增</el-button>
            <el-button type="success" size="small" icon="el-icon-paperclip" @click="back">返 回</el-button>
          </el-col>
        </el-row>
        <!-- 文件上传 -->
      </el-form>
    </el-card>
    <!--表格手动加行-->
    <el-tabs type="border-card">
      <el-tab-pane label="指标明细">
        <el-table :data="newtableData" border style="width: 100%" size="mini">
          <el-table-column type="index" label="序号" align="center" width="80">
          </el-table-column>
          <el-table-column prop="detaolCode" align="center" width="100" label="指标编号">
          </el-table-column>
          <el-table-column prop="detaolName" align="center" show-overflow-tooltip label="指标名称">
          </el-table-column>
          <el-table-column prop="isUse" align="center" width="100" label="是否启用" :formatter="isUseFor">
          </el-table-column>
          <el-table-column prop="serialNum" align="center" width="100" label="显示序号">
          </el-table-column>
          <el-table-column prop="accountDescription" align="center" width="230" show-overflow-tooltip label="计算说明">
          </el-table-column>
          <el-table-column prop="formula" align="center" show-overflow-tooltip label="计算公式">
          </el-table-column>
          <el-table-column align="center" label="操作">
            <template slot-scope="scope">
              <el-button type="text" @click="edit(scope.row)" size="mini">编辑</el-button>
              <el-button type="text" @click="toDel(scope.row)" size="mini" style="color: red;">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>
    <el-dialog :visible.sync="dialogTableVisible" :close-on-click-modal="false">
      <el-form :model="subFormData" ref="subFormData" :rules="rules" label-width="80px">
        <!--基础表新增-->
        <el-form-item label="指标编号" size="small" prop="detaolCode">
          <el-input v-model="subFormData.detaolCode" placeholder="请输入指标编号" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="指标名称" size="small" prop="detaolName">
          <el-input v-model="subFormData.detaolName" placeholder="请输入指标名称" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="计算说明" size="small" prop="accountDescription">
          <el-input v-model="subFormData.accountDescription" placeholder="请输入计算说明" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="计算公式" size="small" prop="formula">
          <el-input v-model="subFormData.formula" placeholder="请输入计算公式" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="显示序号" size="small" prop="serialNum">
          <el-input v-model="subFormData.serialNum" placeholder="请输入排序字段" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否启用" size="small">
          <el-radio-group v-model="subFormData.isUse">
            <el-radio label="1">是</el-radio>
            <el-radio label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="handleFormClose">取 消</el-button>
        <el-button size="small" type="primary" @click="doSave('subFormData')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      rules: {
        detaolCode: [
          {
            required: true,
            message: "请输入指标编号",
            trigger: "blur"
          }
        ],
        detaolName: [
          {
            required: true,
            message: "请输入指标名称",
            trigger: "blur"
          }
        ],
        accountDescription: [
          {
            required: true,
            message: "请输入计算说明",
            trigger: "blur"
          }
        ],
        formula: [
          {
            required: true,
            message: "请输入计算公式",
            trigger: "blur"
          }
        ],
        serialNum: [
          {
            required: true,
            message: "请输入计算公式",
            trigger: "blur"
          }
        ]
      },
      newtableData: [],
      dialogTableVisible: false,
      reportTemplate: {
        setCode: this.$route.query.setCode,
        setName: this.$route.query.setName,
        isUse: this.$route.query.isUse
      },
      subFormData: {
        id: "",
        detaolCode: "",
        detaolName: "",
        isUse: "",
        serialNum: "",
        accountDescription: "",
        formula: ""
      },
      fileList: []
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    //table翻译 求和标识
    isUseFor: function(row, column) {
      return row.isUse == "1" ? "是" : "否";
    },
    // 关闭弹窗
    handleFormClose() {
      this.subFormData.id = "";
      this.subFormData.detaolCode = "";
      this.subFormData.detaolName = "";
      this.subFormData.isUse = "";
      this.subFormData.serialNum = "";
      this.subFormData.accountDescription = "";
      this.subFormData.formula = "";
      this.dialogTableVisible = false;
    },
    //新增
    doAdd() {
      this.subFormData.id = "";
      this.subFormData.detaolCode = "";
      this.subFormData.detaolName = "";
      this.subFormData.isUse = "1";
      this.subFormData.serialNum = "";
      this.subFormData.accountDescription = "";
      this.subFormData.formula = "";
      this.dialogTableVisible = true;
    },
    //查询
    getList() {
      let params = {
        alySetId: this.$route.query.setId
      };
      this.$api
        .formPost("/api/biz/finIndicator/doGetListByDetail", params)
        .then(res => {
          if (res.retHead.code === "0000000") {
            this.newtableData = res.retBody;
          }
        });
    },
    //编辑
    edit(row) {
      console.log(row);
      this.subFormData.id = row.id;
      this.subFormData.detaolCode = row.detaolCode;
      this.subFormData.detaolName = row.detaolName;
      this.subFormData.isUse = row.isUse;
      this.subFormData.serialNum = row.serialNum;
      this.subFormData.accountDescription = row.accountDescription;
      this.subFormData.formula = row.formula;
      this.dialogTableVisible = true;
    },
    // 保存
    doSave(formName) {
      let user = JSON.parse(sessionStorage.getItem("user"));
      let params = {};
      if (this.subFormData.id == "") {
        params = {
          alySetId: this.$route.query.setId,
          detaolCode: this.subFormData.detaolCode,
          detaolName: this.subFormData.detaolName,
          isUse: this.subFormData.isUse,
          serialNum: this.subFormData.serialNum,
          accountDescription: this.subFormData.accountDescription,
          formula: this.subFormData.formula,
          createUserId: user.id,
          createUserName: user.realName,
          createTime: new Date()
        };
      } else {
        params = {
          id: this.subFormData.id,
          detaolCode: this.subFormData.detaolCode,
          detaolName: this.subFormData.detaolName,
          isUse: this.subFormData.isUse,
          serialNum: this.subFormData.serialNum,
          accountDescription: this.subFormData.accountDescription,
          formula: this.subFormData.formula
        };
      }
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$api
            .formPost("/api/biz/finIndicator/doSaveFinDetail", params)
            .then(res => {
              if (res.retHead.code == "0000000") {
                if (this.subFormData.id == "") {
                  this.$message({
                    type: "success",
                    message: "保存成功!"
                  });
                } else {
                  this.$message({
                    type: "success",
                    message: "编辑成功!"
                  });
                }
                this.dialogTableVisible = false;
                this.getList();
              }
            });
        } else {
          return false;
        }
      });
    },

    toDel(row) {
      let that = this;
      this.$confirm("确定删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          that.del(row);
        })
        .catch(() => {});
    },
    del(row) {
      this.$api
        .formPost("/api/biz/finIndicator/doRmByDetail", {
          id: row.id
        })
        .then(res => {
          if (res.retHead.code === "0000000") {
            this.$message({
              type: "info",
              message: "删除成功"
            });
            this.getList();
          } else {
            this.$message({
              type: "info",
              message: res.retHead.message
            });
          }
        });
    },
    back() {
      this.$router.go(-1); //返回上一层
    }
  }
};
</script>


